package com.gale.park.modules.pms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gale.park.common.service.RedisService;
import com.gale.park.modules.pms.dto.SeatConditionDTO;
import com.gale.park.modules.pms.mapper.PmsSeatMapper;
import com.gale.park.modules.pms.model.PmsSeat;
import com.gale.park.modules.pms.service.PmsSeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Gale
 * @since 2022-02-16
 */
@Service
public class PmsSeatServiceImpl extends ServiceImpl<PmsSeatMapper, PmsSeat> implements PmsSeatService {

    @Value("${redis.key.prefix.seat}")
    private static String REDIS_KEY_PREFIX_SEAT_ID;

    @Autowired
    private RedisService redisService;

    @Override
    public PmsSeat getSeatById(String seatId) {

        QueryWrapper<PmsSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(PmsSeat::getSeatId, seatId);

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean create(PmsSeat seat) {
        seat.setSeatId(getNum());

        return this.save(seat);
    }

    @Override
    public Page<PmsSeat> list(SeatConditionDTO condition) {
        LambdaQueryWrapper<PmsSeat> queryWrapper = new LambdaQueryWrapper<>();
        Page<PmsSeat> page = new Page<>(condition.getPageNum(), condition.getPageSize());

        if (StrUtil.isNotBlank(condition.getSeatNum())) {
            queryWrapper.eq(PmsSeat::getSeatNum, condition.getSeatNum());
        }
        if (StrUtil.isNotBlank(condition.getSeatSection())) {
            queryWrapper.eq(PmsSeat::getSeatSection, condition.getSeatSection());
        }

        return this.page(page, queryWrapper);
    }

    @Override
    public boolean deleteByIds(List<String> ids) {
        UpdateWrapper<PmsSeat> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda()
                .in(PmsSeat::getSeatId, ids);

        return this.remove(updateWrapper);
    }

    @Override
    public boolean deleteById(String id) {
        UpdateWrapper<PmsSeat> updateWrapper = new UpdateWrapper<>();

        updateWrapper.lambda()
                .eq(PmsSeat::getSeatId, id);

        return this.remove(updateWrapper);
    }

    @Override
    public PmsSeat updateInfo(String id) {
        QueryWrapper<PmsSeat> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(PmsSeat::getSeatId, id);

        return this.getSeatById(id);
    }

    @Override
    public boolean update(PmsSeat seat) {
        UpdateWrapper<PmsSeat> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(PmsSeat::getSeatId, seat.getSeatId());
        return this.update(seat, updateWrapper);
    }

    @Override
    public void updateStatus(String seatId, int i) {
        UpdateWrapper<PmsSeat> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(PmsSeat::getSeatState, i)
                .eq(PmsSeat::getSeatId, seatId);
        this.update(updateWrapper);
    }

    /**
     * 生成编号：生成规则:8位日期+6位以上自增id；
     *
     * @return
     */
    public String getNum() {
        // 编号
        StringBuilder sb = new StringBuilder();
        // 8位日期
        String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        sb.append(yyyyMMdd);

        // 6位以上自增id
        // redis incr 原子性
        // 适合并发的自增方式：
        String key = REDIS_KEY_PREFIX_SEAT_ID + yyyyMMdd;
        Long incr = redisService.incr(key, 1);
        // 拿到当前的6位以上自增 如果小于6位，自动补全0
        if (incr.toString().length() <= 6) {
            sb.append(String.format("%06d", redisService.incr(key, 1)));
        } else {
            // 如果是6位 不用补0
            sb.append(incr);
        }
        return sb.toString();
    }
}
